﻿
Imports Microsoft.Office.Interop


''' <summary>
''' Excel 資料導出(客服)
''' </summary>
''' <remarks>
''' Create Date:2008/05/14
''' Create Person:huyang
''' Modif Person:Songrenhua
''' Modif Date:2009/06/15
''' </remarks>
''' 

Public Class frm_CSExprot

    Implements IDServer.IDServer.IDServer

#Region "Valiabled Define"

#Region "System Variabled Define"


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Private WithEvents MainForm As Frm_MainForm

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

    Public action_help As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Public FindBool As Boolean = True

#End Region

#Region "user variabled define"

    ''' <summary>
    ''' 窗體是否加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoadBool As Boolean = False

    Public Fields As String

    Private Treedb As DataTable = New DataTable()
    Private Treedb1 As DataTable = New DataTable()

#End Region

#End Region

#Region "Methods"

#Region "Initialize"

    Private Function ReturnInitDs() As DataSet
        Dim ds As DataSet = New DataSet
        Try
            Dim sql As String = ""

            sql &= " select * from (" & vbCrLf
            sql &= " select fCode='01',fFullCode='0',fName='CS來電',fLevel=0" & vbCrLf
            sql &= " union   all" & vbCrLf
            sql &= " select fCode,fFullCode='01',fName,fLevel=1 from dbo.CS_CusterServiceType where fParentCode='05'" & vbCrLf
            sql &= " union  all" & vbCrLf
            sql &= " select fCode='02',fFullCode='0',fName='CS留言',fLevel=2" & vbCrLf
            sql &= " union   all" & vbCrLf
            sql &= " select fCode,fFullCode='02',fName,fLevel=3 from dbo.CS_CusterServiceType where fParentCode='05'" & vbCrLf
            sql &= " ) a order by fLevel,fCode" & vbCrLf

            sql &= " select fCode,fName from dbo.CS_CusterServiceType where  fCode in ('0201','0202','0203','0204','0211','0212') " & vbCrLf

            sql &= "  select fCode,fName from dbo.CS_CusterServiceType where  fCode in ('0205','0206','0207','0208')  " & vbCrLf

            sql &= "  select * from (" & vbCrLf
            sql &= "  select fCode='01',fFullCode='0',fName='華方來電',fLevel=0" & vbCrLf
            sql &= "  union   all" & vbCrLf
            sql &= "  select fCode,fFullCode='01',fName,fLevel=1 from dbo.CS_CusterServiceType where fParentCode='1201'" & vbCrLf
            sql &= "  union  all" & vbCrLf
            sql &= "  select fCode='02',fFullCode='0',fName='優質來電',fLevel=2" & vbCrLf
            sql &= "  union   all" & vbCrLf
            sql &= "  select fCode,fFullCode='02',fName,fLevel=3 from dbo.CS_CusterServiceType where fParentCode='1202'" & vbCrLf
            sql &= "  union  all" & vbCrLf
            sql &= "  select fCode='03',fFullCode='0',fName='偉倫安裝',fLevel=4" & vbCrLf
            sql &= "  union   all" & vbCrLf
            sql &= "  select fCode,fFullCode='03',fName,fLevel=5 from dbo.CS_CusterServiceType where fParentCode='1203'" & vbCrLf
            sql &= "  ) a order by fLevel,fCode" & vbCrLf

            ds = Exec(sql, E_ExecType.DataSet)

        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return ds
    End Function

    Private Sub Initialize()

        Me.dtp_star_1.Text = ServerDate
        Me.dtp_end_1.Text = ServerDate
        Me.dtp_start_2.Text = ServerDate
        Me.dtp_end_2.Text = ServerDate
        Me.dtp_start_3.Text = ServerDate
        Me.dtp_end_3.Text = ServerDate

        FillComboxForEnum(Me.cbxfLevel, GetType(ModEnum.ComplainLevel))
        FillComboxForEnum(Me.ComboBox1, GetType(ModEnum.CallBackStatus))
        FillComboxForEnum(Me.ComboBox4, GetType(ModEnum.CompleteStatus))

        FillComboxForEnum(Me.ComboBox5, GetType(ModEnum.CallBackStatus))
        FillComboxForEnum(Me.ComboBox6, GetType(ModEnum.CompleteStatus))

        FillComboxForEnum(Me.ComboBox8, GetType(ModEnum.CallBackStatus))
        FillComboxForEnum(Me.ComboBox7, GetType(ModEnum.CompleteStatus))
        FillComboxForEnum(Me.ComboBox9, GetType(ModEnum.CompleteStatus))
        FillComboxForEnum(Me.ComboBox10, GetType(ModEnum.CallBackStatus))
        FillComboxForEnum(Me.ComboBox11, GetType(ModEnum.ComplainLevel))

        ''E_AgentType
        FillComboxForEnum(Me.cbxSupplierName, GetType(ModEnum.E_AgentType))
        FillComboxForEnum(Me.ComboBox12, GetType(ModEnum.E_AgentType))

        FillComboxForEnum(Me.ComboBox2, GetType(ModEnum.E_AgentType))
        FillComboxForEnum(Me.cbxAgentCode, GetType(ModEnum.E_AgentType))

        Dim ds As DataSet = Me.ReturnInitDs()
        ds = Me.ReturnInitDs()
        Me.Treedb = ds.Tables(0).Copy()
        FillDeliverType(ds.Tables(1).Copy())
        FillCombox3(ds.Tables(2).Copy())
        Me.Load_TreeView(Me.TreeView1, "全部")
        Me.TreeView1.Visible = False

        Me.Treedb1 = ds.Tables(3).Copy()
        Me.Load_TreeView(Me.TreeView2, "全部")
        Me.TreeView2.Visible = False

        Dim imeMode As clsIme = New clsIme()
        imeMode.AddHandler_ImeMode(New Control() {Me.Panel1, Me.Panel3, Panel5})
    End Sub

    Private Sub FillDeliverType(ByVal dtab As DataTable)
        Me.cbxDeliveryType.DataSource = dtab
        Me.cbxDeliveryType.DisplayMember = "fName"
        Me.cbxDeliveryType.ValueMember = "fCode"
    End Sub

    Private Sub FillCombox3(ByVal Dtab As DataTable)
        Me.ComboBox3.DataSource = Dtab
        Me.ComboBox3.DisplayMember = "fName"
        Me.ComboBox3.ValueMember = "fCode"
    End Sub

#End Region

#Region "TreeView"

    ''' <summary>
    ''' 加載首結點
    ''' </summary>
    ''' <param name="Tvw"></param>
    ''' <param name="title"></param>
    ''' <remarks></remarks>
    Private Sub Load_TreeView(ByRef Tvw As TreeView, ByVal title As String)
        Tvw.Nodes.Clear()
        Dim Node As TreeNode = Tvw.Nodes.Add(title)
        Node.Tag = "0"
        Node.Nodes.Add("...")
        Node.Expand()
        Tvw.SelectedNode = Node
    End Sub

    Private Sub Load_TreeNode(ByVal Node As TreeNode, ByVal Dtab As DataTable)
        Try
            If Node Is Nothing Then Exit Sub
            If Node.Nodes.Count > 0 AndAlso Node.Nodes(0).Text.Trim <> "..." Then Exit Sub

            Node.Nodes.Clear()
            Dim fLevel As Integer = Node.Level
            Dim RowFilter As String = ""
            Dim fCode As String = Node.Tag.ToString.Split(":")(0)

            Dim whereExp As String = "fFullCode='" & fCode & "'"
            Dim DRow() As DataRow = Dtab.Select(whereExp, "fCode")

            For Each row As DataRow In DRow
                Dim key As String = row("fCode") & ""
                Dim text As String = row("fName") & ""
                Dim TempNode As TreeNode = Node.Nodes.Add(text)
                TempNode.Tag = key
                Dim srow() As DataRow = Dtab.Select("fFullCode='" & key & "'")
                If srow.Length > 0 Then
                    TempNode.Nodes.Add("...")
                End If
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "ExecFind"

#Region "Find_1"

    Private Function ReturnWhere_1() As String
        Dim sql As String = ""

        If Me.fLoadBool = False Then
            sql &= "  where 1<>1 "
        Else
            sql &= "   where 1=1 and not exists (select * from CS_CusterServiceTypes where fCode=ItemParentNo) "

            If Not Me.TreeView1.SelectedNode Is Nothing Then
                Dim Node As TreeNode = Me.TreeView1.SelectedNode
                Dim key As String = Node.Tag & ""
                Dim parentKey As String = ""
                If Not Node.Parent Is Nothing Then
                    parentKey = Node.Parent.Tag & ""
                End If
                Select Case Node.Level
                    Case 0
                        sql &= " and (Isnull(ItemParentNo,'')='05'  or (Isnull(ItemParentNo,'')='11' and Isnull(ItemNo,'')='1101'  and Isnull(ItemParentNo2,'')='05'  )) " & vbCrLf
                    Case 1

                        If key.Trim = "01" Then
                            sql &= " and Isnull(ItemParentNo,'')='05'" & vbCrLf
                        Else
                            sql &= " and ( Isnull(ItemParentNo,'')='11' and Isnull(ItemNo,'')='1101'  and Isnull(ItemParentNo2,'')='05')" & vbCrLf
                        End If

                    Case 2

                        If parentKey.Trim = "01" Then
                            sql &= " and Isnull(ItemParentNo,'')='05'   and Isnull(ItemNo,'')='" & key & "' " & vbCrLf
                        Else
                            sql &= " and (Isnull(ItemParentNo,'')='11' and Isnull(ItemNo,'')='1101'  and Isnull(ItemParentNo2,'')='05'  and Isnull(ItemNo2,'')='" & key & "') " & vbCrLf
                        End If

                End Select
            Else
                sql &= " and (Isnull(ItemParentNo,'')='05'  or (Isnull(ItemParentNo,'')='11' and Isnull(ItemNo,'')='1101'  and Isnull(ItemParentNo2,'')='05'  )) " & vbCrLf
            End If
      
            sql &= "  and ReceiveCallDate>='" & Format(Me.dtp_star_1.Value, "yyyy-MM-dd") & "'"
            sql &= "  and ReceiveCallDate<='" & Format(Me.dtp_end_1.Value, "yyyy-MM-dd") & "'"

            If Me.cbxfLevel.Text.Trim <> "" Then
                sql &= " and Isnull(ComplainLevel,'一般')='" & Me.cbxfLevel.Text & "'"
            End If

            If Me.cbxSupplierName.Text.Trim <> "" Then
                If [Enum].IsDefined(GetType(ModEnum.E_AgentType), Me.cbxSupplierName.Text.Trim) = True Then
                    Select Case Me.cbxSupplierName.Text.Trim
                        Case ModEnum.E_AgentType.A貨.ToString
                            sql &= "  and Isnull(a.SupplierName,'')='A貨' "
                        Case ModEnum.E_AgentType.行貨.ToString
                            sql &= "  and Isnull(a.SupplierName,'')<>'A貨'  and Isnull(a.SupplierName,'')<>'' "
                        Case ModEnum.E_AgentType.未填.ToString
                            sql &= "  and Isnull(a.SupplierName,'')=''  "
                    End Select
                Else
                    sql &= "  and Isnull(a.SupplierName,'')='" & Me.cbxSupplierName.Text.Trim & "' "
                End If

            End If

            If Me.ComboBox5.Text.Trim <> "" Then
                If Me.ComboBox5.Text.Trim = ModEnum.CallBackStatus.已回複.ToString Then
                    sql &= "  and Isnull(CallBackStatus,0)=1  " & vbCrLf
                Else
                    sql &= "  and Isnull(CallBackStatus,0)=0  " & vbCrLf
                End If
            End If

            If Me.ComboBox6.Text.Trim <> "" Then
                If Me.ComboBox6.Text.Trim = CompleteStatus.已完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0)" & vbCrLf
                ElseIf Me.ComboBox6.Text.Trim = CompleteStatus.未完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0) " & vbCrLf
                ElseIf Me.ComboBox6.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
                    sql &= "  and (Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0)" & vbCrLf
                Else
                    sql &= "  and (Charindex('" & Me.ComboBox6.Text.Trim & "',Isnull(LastCompleteRemark,''),0)>0 or  charindex('" & Me.ComboBox6.Text.Trim & "',Isnull(CompleteRemark,''),0)>0) " & vbCrLf
                End If
            End If

            If Me.txtListenPsn_1.Text.Trim <> "" AndAlso Me.txtLastCallBackPsn_1.Text.Trim <> "" Then
                sql &= "  and (ListenPsn='" & Me.txtListenPsn_1.Text.Trim & "'   or a.BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn_1.Text.Trim & "'))" & vbCrLf
            Else
                If Me.txtListenPsn_1.Text <> "" Then
                    sql &= "  and ListenPsn='" & Me.txtListenPsn_1.Text.Trim & "' " & vbCrLf
                End If
                If Me.txtLastCallBackPsn_1.Text.Trim <> "" Then
                    'sql &= " and LastCallBackPsn='" & Me.txtLastCallBackPsn_1.Text.Trim & "'"
                    sql &= "   and BoNum in (select  Distinct a.BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn_1.Text.Trim & "')" & vbCrLf
                End If
            End If

            ''負責人
            If Me.TextBox3.Text.Trim <> "" Then
                sql &= "   and Isnull(LastCallBackPsn,ListenPsn)='" & Me.TextBox3.Text.Trim & "' "
            End If

            ''  sql &= "  and Isnull(fMulti,0)=0 " & vbCrLf
        End If

        Return sql
    End Function

    Private Function ReturnWhere_1_2() As String
        Dim sql As String = ""

        If Me.fLoadBool = False Then
            sql &= "  where 1<>1 "
        Else
            sql &= "   where 1=1 and not exists (select * from CS_CusterServiceTypes where fCode=b.ReceiveCode) "

            If Not Me.TreeView1.SelectedNode Is Nothing Then
                Dim Node As TreeNode = Me.TreeView1.SelectedNode
                Dim key As String = Node.Tag & ""
                Dim parentKey As String = ""
                If Not Node.Parent Is Nothing Then
                    parentKey = Node.Parent.Tag & ""
                End If
                Select Case Node.Level
                    Case 0
                        sql &= " and Isnull(b.ReceiveCode,'') in (select fCode from CS_CusterServiceType where fParentCode='05')" & vbCrLf
                    Case 1

                        If key.Trim = "01" Then
                            sql &= " and  Isnull(b.ReceiveCode,'') in (select fCode from CS_CusterServiceType where fParentCode='05') " & vbCrLf
                        Else
                            sql &= " and Isnull(ItemNo,'')='1101'  and Isnull(ItemParentNo2,'')='05' and  Isnull(b.ReceiveCode,'') in (select fCode from CS_CusterServiceType where fParentCode='05') "
                        End If

                    Case 2

                        If parentKey.Trim = "01" Then
                            sql &= " and Isnull(b.ReceiveCode,'')='" & key & "' "
                        Else
                            sql &= " and Isnull(b.ReceiveCode,'')='" & key & "' "
                        End If

                End Select
            Else
                sql &= "   and  Isnull(b.ReceiveCode,'') in (select fCode from CS_CusterServiceType where fParentCode='05')" & vbCrLf
            End If

            sql &= "  and ReceiveCallDate>='" & Format(Me.dtp_star_1.Value, "yyyy-MM-dd") & "'"
            sql &= "  and ReceiveCallDate<='" & Format(Me.dtp_end_1.Value, "yyyy-MM-dd") & "'"

            If Me.cbxfLevel.Text.Trim <> "" Then
                sql &= " and Isnull(ComplainLevel,'一般')='" & Me.cbxfLevel.Text & "'"
            End If

            If Me.cbxSupplierName.Text.Trim <> "" Then
                If [Enum].IsDefined(GetType(ModEnum.E_AgentType), Me.cbxSupplierName.Text.Trim) = True Then
                    Select Case Me.cbxSupplierName.Text.Trim
                        Case ModEnum.E_AgentType.A貨.ToString
                            sql &= "  and Isnull(b.SupplierName,'')='A貨' "
                        Case ModEnum.E_AgentType.行貨.ToString
                            sql &= "  and Isnull(b.SupplierName,'')<>'A貨'  and Isnull(b.SupplierName,'')<>'' "
                        Case ModEnum.E_AgentType.未填.ToString
                            sql &= "  and Isnull(b.SupplierName,'')=''  "
                    End Select
                Else
                    sql &= "  and Isnull(b.SupplierName,'')='" & Me.cbxSupplierName.Text.Trim & "' "
                End If

            End If

            If Me.ComboBox5.Text.Trim <> "" Then
                If Me.ComboBox5.Text.Trim = ModEnum.CallBackStatus.已回複.ToString Then
                    sql &= "  and Isnull(CallBackStatus,0)=1  "
                Else
                    sql &= "  and Isnull(CallBackStatus,0)=0  "
                End If
            End If

            'If Me.ComboBox6.Text.Trim <> "" Then
            '    If Me.ComboBox6.Text.Trim = CompleteStatus.已完成.ToString() Then
            '        sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0)" & vbCrLf
            '    ElseIf Me.ComboBox6.Text.Trim = CompleteStatus.未完成.ToString() Then
            '        sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0) " & vbCrLf
            '    ElseIf Me.ComboBox6.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
            '        sql &= "  and (Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0)" & vbCrLf
            '    Else
            '        sql &= "  and (Charindex('" & Me.ComboBox6.Text.Trim & "',Isnull(LastCompleteRemark,''),0)>0 or  charindex('" & Me.ComboBox6.Text.Trim & "',Isnull(CompleteRemark,''),0)>0) " & vbCrLf
            '    End If
            'End If

            If Me.ComboBox6.Text.Trim <> "" Then
                If Me.ComboBox6.Text.Trim = CompleteStatus.已完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(b.CompleteStatus,''),0)>0 or Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0)"
                ElseIf Me.ComboBox6.Text.Trim = CompleteStatus.未完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(b.CompleteStatus,''),0)<=0  and Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0)"
                ElseIf Me.ComboBox6.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
                    sql &= "  and (Charindex('跟進',Isnull(b.CompleteStatus,''),0)>0 or Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0 and Charindex('ok',Isnull(b.CompleteStatus,''),0)<=0 )" & vbCrLf
                Else
                    sql &= "  and Charindex('" & Me.ComboBox6.Text.Trim & "',Isnull(b.CompleteStatus,''),0)>0  "
                End If
            End If

            If Me.txtListenPsn_1.Text.Trim <> "" AndAlso Me.txtLastCallBackPsn_1.Text.Trim <> "" Then
                sql &= "  and (ListenPsn='" & Me.txtListenPsn_1.Text.Trim & "'   or a.BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn_1.Text.Trim & "'))"
            Else
                If Me.txtListenPsn_1.Text <> "" Then
                    sql &= "  and ListenPsn='" & Me.txtListenPsn_1.Text.Trim & "' "
                End If
                If Me.txtLastCallBackPsn_1.Text.Trim <> "" Then
                    'sql &= " and LastCallBackPsn='" & Me.txtLastCallBackPsn_1.Text.Trim & "'"
                    sql &= "   and a.BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn_1.Text.Trim & "')"
                End If
            End If

            ''負責人
            If Me.TextBox3.Text.Trim <> "" Then
                sql &= "   and Isnull(LastCallBackPsn,ListenPsn)='" & Me.TextBox3.Text.Trim & "' "
            End If

        End If

        Return sql
    End Function

    Public Sub ExecFind_1(ByVal whereExp As String, ByVal whereExp2 As String)

        Me.Cursor = Cursors.WaitCursor

        Dim sql As String = ""
        sql &= "  "
        sql &= "  select distinct ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),ReceiveCallTime,fSaleOrdeNo,PurchaseDate=Replace(PurchaseDate,'-','/'),SupplierName,GoodsName,a.enname,GoodsDescript," & vbCrLf
        sql &= "  Contents,fEmployeeName,DSCTruck,DGKD,DGDelivery,CustomerName,ContactPhone," & vbCrLf
        sql &= "  LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,LastCallBackRemark=Case When Isnull(LastCallBackRemark,'')='' then Remark else LastCallBackRemark End,CompleteRemark=Case when Isnull(LastCompleteRemark,'')=''   then CompleteRemark else LastCompleteRemark  end ,LastCallBackDate=Case when  (charindex('ok',CompleteRemark,0)>0 or charindex('ok',LastCompleteRemark,0)>0) then (case when Isnull(LastCallBackDate,'')='' then Replace(ReceiveCallDate,'-','/')  else Replace(LastCallBackDate,'-','/')   end) else Null end    from CS_ReceiveInformation  a " & whereExp & vbCrLf
        sql &= " Union all" & vbCrLf

        sql &= " select  ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),ReceiveCallTime,b.fSaleOrdeNo,PurchaseDate=Replace(b.PurchaseDate,'-','/'),b.SupplierName,b.GoodsName,a.enname,b.GoodsDescript," & vbCrLf
        sql &= " Contents=case when Isnull(Contents2,'')<>'' then Contents2 when Isnull(Remark2,'')<>'' then Remark2  else Contents end  ,fEmployeeName,case when isnull(b.DSCTruck,'')='' then a.DSCTruck else b.DSCTruck end as DSCTruck" + vbNewLine
        sql += ",case when isnull(b.DGKD,'')='' then a.DGKD  else b.DGKD end as DGKD,case when isnull(b.DGDelivery,'')='' then a.DGDelivery else b.DGDelivery end as DGDelivery,CustomerName,ContactPhone," & vbCrLf
        sql &= " LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End," & vbCrLf
        sql &= " LastCallBackRemark=Case When Isnull(LastCallBackRemark,'')='' then Remark else LastCallBackRemark End," & vbCrLf
        sql &= " CompleteRemark=Case when Isnull(LastCompleteRemark,'')=''   then CompleteRemark else LastCompleteRemark  " & vbCrLf
        sql &= " end ,LastCallBackDate=Case when  (charindex('ok',CompleteRemark,0)>0 or charindex('ok',LastCompleteRemark,0)>0) then (case when Isnull(LastCallBackDate,'')='' then Replace(ReceiveCallDate,'-','/')  else Replace(LastCallBackDate,'-','/')   end) else Null end    " & vbCrLf
        sql &= " from CS_ReceiveInformation  a" & vbCrLf
        sql &= " inner join CS_ReceiveSaleInfo b on a.BoNum=b.BoNum  " & whereExp2 & vbCrLf

        Try
            Dim dtab As New DataTable
            dtab = Exec(sql, E_ExecType.DataTable)
            Me.dg_complaint.DataSource = dtab
            BindDataGridViewCaption(Me.dg_complaint, Me.ReturnCaption(1))
            FullFormatDataGridView(Me.dg_complaint, False, False)
            Me.dg_complaint.SelectionMode = DataGridViewSelectionMode.CellSelect
            SetDataGridWidth_1()
            If Me.fLoadBool = True Then
                SetButton(ButtonType.Browse, False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function ReturnCaption(ByVal Index) As Hashtable
        Dim Hash As New Hashtable

        Select Case Index
            Case 1

                Hash.Add("ReceiveCallDate", "投訴日期")
                Hash.Add("ReceiveCallTime", "來電時間")
                Hash.Add("fSaleOrdeNo", "單據號碼")
                Hash.Add("PurchaseDate", "購買日期")
                Hash.Add("SupplierName", "供應商")
                Hash.Add("GoodsName", "貨品型號")
                Hash.Add("enname", "BarCode")
                Hash.Add("GoodsDescript", "貨品描述")
                Hash.Add("Contents", "顧客投訴事項")
                Hash.Add("fEmployeeName", "員工編號/姓名")
                Hash.Add("DSCTruck", "DSC運輸車")
                Hash.Add("DGKD", "DG 柯打")
                Hash.Add("DGDelivery", "DG 送貨")
                Hash.Add("CustomerName", "顧客姓名")
                Hash.Add("ContactPhone", "顧客電話")
                Hash.Add("LastCallBackPsn", "負責人")
                Hash.Add("LastCallBackRemark", "解決方案")
                Hash.Add("CompleteRemark", "跟進備註")
                Hash.Add("LastCallBackDate", "完成日期")

            Case 2

                Hash.Add("ReceiveCallDate", "客來電時間")
                Hash.Add("fSaleOrdeNo", "銷售單號碼")
                Hash.Add("DSCTruck", "車隊")
                Hash.Add("DeliveryAddress", "送貨地址")
                Hash.Add("ListenPsn", "CS負責人")
                Hash.Add("LastCallBackPsn", "跟進人")
                Hash.Add("Contents", "查詢內容")
                Hash.Add("LastCallBackRemark", "回複送貨日期及資料")
                Hash.Add("LastCallBackDate", "回覆時間")

            Case 3

                Hash.Add("ReceiveCallDate", "客來電時間")
                Hash.Add("SupplierName", "代理行")
                Hash.Add("PurchaseDate", "開單日期")
                Hash.Add("fSaleOrdeNo", "銷售單號碼")
                Hash.Add("GoodsName", "産品型號")
                Hash.Add("GoodsDescript", "貨品描述")
                Hash.Add("DGDelivery", "送貨地址")
                Hash.Add("ListenPsn", "負責人")
                Hash.Add("Contents", "備註")
                Hash.Add("LastCallBackRemark", "回複資料")

            Case 4

                Hash.Add("Id", "序號")
                Hash.Add("ReceiveCallDate", "來電日期")
                Hash.Add("fSaleOrdeNo", "銷售單號")
                Hash.Add("SupplierName", "代理行")
                Hash.Add("fGoodsType", "類別")
                Hash.Add("ListenPsn", "負責人")
                Hash.Add("GoodsDescript", "貨品描述")

            Case 41

                Hash.Add("ReceiveCallDate", "落維修單日期")
                Hash.Add("fSaleOrdeNo", "購貨單編號")
                Hash.Add("fWriteNo", "手寫單號")
                Hash.Add("PurchaseDate", "購買/送貨日期")
                Hash.Add("BaoYangDate", "保養到期日")
                Hash.Add("GoodsName", "種類")
                Hash.Add("SupplierName", "牌子")
                Hash.Add("GoodsDescript", "型號")
                Hash.Add("MachineNo", "機身編碼")
                Hash.Add("CustomerName", "顧客姓名")
                Hash.Add("DeliveryAddress", "地 址")
                Hash.Add("ContactPhone", "電話1")
                Hash.Add("ContactPhone2", "電話2")
                Hash.Add("ContactPhone3", "電話3")
                Hash.Add("CanKaoNo", "參考編號")
                Hash.Add("Contents", "顧客備註(顯示在單據上)")
                Hash.Add("JianChaFei", "檢查費")
                Hash.Add("FeiYong", "費用")
                Hash.Add("LastCallBackPsn", "經手人")
                Hash.Add("flevel1", "內部備註")

            Case 42

                Hash.Add("ReceiveCallDate", "查詢故障及使用方法日期")
                Hash.Add("fSaleOrdeNo", "購貨單編號")
                Hash.Add("fWriteNo", "手寫單號")
                Hash.Add("GoodsName", "種類")
                Hash.Add("SupplierName", "牌子")
                Hash.Add("GoodsDescript", "型號")
                Hash.Add("CustomerName", "顧客姓名")
                Hash.Add("ContactPhone", "電話1")
                Hash.Add("ContactPhone2", "電話2")
                Hash.Add("ContactPhone3", "電話3")
                Hash.Add("Contents", "顧客備註")
                Hash.Add("LastCallBackPsn", "經手人")

            Case 43

                Hash.Add("ReceiveCallDate", "客人來電日期")
                Hash.Add("fSaleOrdeNo", "銷售單號/維修單號")
                Hash.Add("GoodsName", "種類")
                Hash.Add("SupplierName", "牌子")
                Hash.Add("GoodsDescript", "型號")
                Hash.Add("Contents", "內容")
                Hash.Add("LastCallBackPsn", "接聽人")


        End Select

        Return Hash
    End Function

    Private Sub SetDataGridWidth_1()
        Me.dg_complaint.Columns("ReceiveCallDate").Width = 65
        Me.dg_complaint.Columns("fSaleOrdeNo").Width = 80
        Me.dg_complaint.Columns("PurchaseDate").Width = 65
        Me.dg_complaint.Columns("SupplierName").Width = 60
        Me.dg_complaint.Columns("GoodsName").Width = 60
        Me.dg_complaint.Columns("Contents").Width = 200
        Me.dg_complaint.Columns("fEmployeeName").Width = 60
        Me.dg_complaint.Columns("DSCTruck").Width = 60
        Me.dg_complaint.Columns("DGKD").Width = 60
        Me.dg_complaint.Columns("DGDelivery").Width = 60
        Me.dg_complaint.Columns("CustomerName").Width = 60
        Me.dg_complaint.Columns("ContactPhone").Width = 80
        Me.dg_complaint.Columns("LastCallBackPsn").Width = 60
        Me.dg_complaint.Columns("LastCallBackRemark").Width = 200
        Me.dg_complaint.Columns("CompleteRemark").Width = 60
        Me.dg_complaint.Columns("LastCallBackDate").Width = 65

    End Sub

#End Region

#Region "Find_2"

    Private Function ReturnWhereExp() As String
        Dim sql As String = ""
        If Me.fLoadBool = False Then

            sql &= "   where 1<>1 "
        Else

            sql &= "  where 1=1 and not exists (select * from CS_CusterServiceTypes where fCode=ItemParentNo)" + vbNewLine
            If Me.cbxDeliveryType.SelectedValue & "" <> "" Then
                sql &= "  and (Isnull(ItemNo,'')='" & Me.cbxDeliveryType.SelectedValue & "'    or  Isnull(ItemNo2,'')='" & Me.cbxDeliveryType.SelectedValue & "' )" + vbNewLine
            Else
                sql &= "  and (Isnull(ItemNo,'') in ('0201','0202','0203','0204','0211','0212')  or Isnull(ItemNo2,'') in ('0201','0202','0203','0204','0211','0212'))    " + vbNewLine
            End If
            sql &= "      and  ReceiveCallDate>='" & Format(Me.dtp_start_2.Value, "yyyy-MM-dd") & "'   " + vbNewLine
            sql &= "      and  ReceiveCallDate<='" & Format(Me.dtp_end_2.Value, "yyyy-MM-dd") & "'    " + vbNewLine

            If Me.ComboBox1.Text.Trim <> "" Then
                If Me.ComboBox1.Text.Trim = ModEnum.CallBackStatus.已回複.ToString Then
                    sql &= "  and Isnull(CallBackStatus,0)=1  " + vbNewLine
                Else
                    sql &= "  and Isnull(CallBackStatus,0)=0  " + vbNewLine
                End If
            End If
            'If Me.txtListenPsn.Text.Trim <> "" Then
            '    sql &= "  and Isnull(ListenPsn,'')='" & Me.txtListenPsn.Text.Trim & "'  "
            'End If
            'If Me.txtLastCallBackPsn.Text.Trim <> "" Then
            '    sql &= "  and Isnull(LastCallBackPsn,'')='" & Me.txtLastCallBackPsn.Text.Trim & "'  "
            'End If

            If Me.txtListenPsn.Text.Trim <> "" AndAlso Me.txtLastCallBackPsn.Text.Trim <> "" Then
                sql &= "  and (ListenPsn='" & Me.txtListenPsn.Text.Trim & "'   or BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn.Text.Trim & "'))" + vbNewLine
            Else
                If Me.txtListenPsn.Text <> "" Then
                    sql &= "  and ListenPsn='" & Me.txtListenPsn.Text.Trim & "' " + vbNewLine
                End If
                If Me.txtLastCallBackPsn.Text.Trim <> "" Then
                    'sql &= " and LastCallBackPsn='" & Me.txtLastCallBackPsn_1.Text.Trim & "'"
                    sql &= "   and BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn.Text.Trim & "')" + vbNewLine
                End If
            End If

            If Me.ComboBox2.Text.Trim <> "" Then
                If [Enum].IsDefined(GetType(ModEnum.E_AgentType), Me.ComboBox2.Text.Trim) = True Then
                    Select Case Me.ComboBox2.Text.Trim
                        Case ModEnum.E_AgentType.A貨.ToString
                            sql &= "  and Isnull(SupplierName,'')='A貨' " + vbNewLine
                        Case ModEnum.E_AgentType.行貨.ToString
                            sql &= "  and Isnull(SupplierName,'')<>'A貨'  and Isnull(SupplierName,'')<>'' " + vbNewLine
                        Case ModEnum.E_AgentType.未填.ToString
                            sql &= "  and Isnull(SupplierName,'')=''  " + vbNewLine
                    End Select
                Else
                    sql &= "  and Isnull(SupplierName,'')='" & Me.ComboBox2.Text.Trim & "'  " + vbNewLine
                End If
            End If


            If Me.ComboBox4.Text.Trim <> "" Then

                If Me.ComboBox4.Text.Trim = CompleteStatus.已完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0)" + vbNewLine
                ElseIf Me.ComboBox4.Text.Trim = CompleteStatus.未完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0) " + vbNewLine
                ElseIf Me.ComboBox4.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
                    sql &= "  and (Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0)" + vbNewLine
                Else
                    sql &= "  and (Charindex('" & Me.ComboBox4.Text.Trim & "',Isnull(LastCompleteRemark,''),0)>0 or  charindex('" & Me.ComboBox4.Text.Trim & "',Isnull(CompleteRemark,''),0)>0) " + vbNewLine
                End If
            End If

            If Me.TextBox1.Text.Trim <> "" Then
                sql &= " and convert(int,substring(ReceiveCallTime,1,case when charindex(':',ReceiveCallTime)-1 < 0 then 0 else charindex(':',ReceiveCallTime)-1 end))>= " & Val(Me.TextBox1.Text.Trim) & "   " + vbNewLine
            End If

            If Me.TextBox2.Text.Trim <> "" Then

                sql &= " and convert(int,substring(ReceiveCallTime,1,case when charindex(':',ReceiveCallTime)-1 < 0 then 0 else charindex(':',ReceiveCallTime)-1 end))<= " & Val(Me.TextBox2.Text.Trim) & "   " + vbNewLine
            End If

            ''負責人
            If Me.TextBox4.Text.Trim <> "" Then
                sql &= "   and Isnull(LastCallBackPsn,ListenPsn)='" & Me.TextBox4.Text.Trim & "' " + vbNewLine
            End If
        End If
        Return sql
    End Function

    Private Function ReturnWhereExp_2() As String
        Dim sql As String = ""
        If Me.fLoadBool = False Then
            sql &= "   where 1<>1 "
        Else
            sql &= "  where 1=1 and not exists (select * from CS_CusterServiceTypes where fCode=b.ReceiveCode) " + vbNewLine
            If Me.cbxDeliveryType.SelectedValue & "" <> "" Then
                'sql &= " and  (a.ItemParentNo='02' or (a.ItemParentNo='11' and ItemNo='1101' and ItemParentNo2='02'))" & vbCrLf
                ''(Isnull(ItemNo,'')='" & Me.cbxDeliveryType.SelectedValue & "'    or  Isnull(ItemNo2,'')='" & Me.cbxDeliveryType.SelectedValue & "' )"
                sql &= " and  (Isnull(b.ReceiveCode,'')='" & Me.cbxDeliveryType.SelectedValue & "" & "'  or (Isnull(b.ReceiveCode,'')='' and (Isnull(ItemNo,'')='" & Me.cbxDeliveryType.SelectedValue & "'    or  Isnull(ItemNo2,'')='" & Me.cbxDeliveryType.SelectedValue & "')) ) " & vbCrLf
            Else
                'sql &= "  and  (a.ItemParentNo='02' or (a.ItemParentNo='11' and ItemNo='1101' and ItemParentNo2='02'))" & vbCrLf
                sql &= "  and (Isnull(b.ReceiveCode,'')  in ('0201','0202','0203','0204'))    " & vbCrLf
            End If

            sql &= "      and  ReceiveCallDate>='" & Format(Me.dtp_start_2.Value, "yyyy-MM-dd") & "'   " + vbNewLine
            sql &= "      and  ReceiveCallDate<='" & Format(Me.dtp_end_2.Value, "yyyy-MM-dd") & "'    " + vbNewLine

            If Me.ComboBox1.Text.Trim <> "" Then
                If Me.ComboBox1.Text.Trim = ModEnum.CallBackStatus.已回複.ToString Then
                    sql &= "  and Isnull(CallBackStatus,0)=1  " + vbNewLine
                Else
                    sql &= "  and Isnull(CallBackStatus,0)=0  " + vbNewLine
                End If
            End If


            If Me.txtListenPsn.Text.Trim <> "" AndAlso Me.txtLastCallBackPsn.Text.Trim <> "" Then
                sql &= "  and (ListenPsn='" & Me.txtListenPsn.Text.Trim & "'   or a.BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn.Text.Trim & "'))" + vbNewLine
            Else
                If Me.txtListenPsn.Text <> "" Then
                    sql &= "  and ListenPsn='" & Me.txtListenPsn.Text.Trim & "' " + vbNewLine
                End If
                If Me.txtLastCallBackPsn.Text.Trim <> "" Then
                    sql &= "   and a.BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn.Text.Trim & "')" + vbNewLine
                End If
            End If

            If Me.ComboBox2.Text.Trim <> "" Then
                If [Enum].IsDefined(GetType(ModEnum.E_AgentType), Me.ComboBox2.Text.Trim) = True Then
                    Select Case Me.ComboBox2.Text.Trim
                        Case ModEnum.E_AgentType.A貨.ToString
                            sql &= "  and Isnull(b.SupplierName,'')='A貨' " + vbNewLine
                        Case ModEnum.E_AgentType.行貨.ToString
                            sql &= "  and Isnull(b.SupplierName,'')<>'A貨'  and Isnull(b.SupplierName,'')<>'' " + vbNewLine
                        Case ModEnum.E_AgentType.未填.ToString
                            sql &= "  and Isnull(b.SupplierName,'')=''  " + vbNewLine
                    End Select
                Else
                    sql &= "  and Isnull(b.SupplierName,'')='" & Me.ComboBox2.Text.Trim & "'  " + vbNewLine
                End If

            End If


            'If Me.ComboBox4.Text.Trim <> "" Then
            '    If Me.ComboBox4.Text.Trim = CompleteStatus.已完成.ToString() Then
            '        sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0)"
            '    ElseIf Me.ComboBox4.Text.Trim = CompleteStatus.未完成.ToString() Then
            '        sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0) "
            '    ElseIf Me.ComboBox4.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
            '        sql &= "  and (Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0)"
            '    Else
            '        sql &= "  and (Charindex('" & Me.ComboBox4.Text.Trim & "',Isnull(LastCompleteRemark,''),0)>0 or  charindex('" & Me.ComboBox4.Text.Trim & "',Isnull(CompleteRemark,''),0)>0) "
            '    End If
            'End If
            If Me.ComboBox4.Text.Trim <> "" Then
                If Me.ComboBox4.Text.Trim = CompleteStatus.已完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(b.CompleteStatus,''),0)>0 or Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0) " + vbNewLine
                ElseIf Me.ComboBox4.Text.Trim = CompleteStatus.未完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(b.CompleteStatus,''),0)<=0  and Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0 )" + vbNewLine
                ElseIf Me.ComboBox4.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
                    sql &= "  and (Charindex('跟進',Isnull(b.CompleteStatus,''),0)>0 or Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0 and Charindex('ok',Isnull(b.CompleteStatus,''),0)<=0) " + vbNewLine
                Else
                    sql &= "  and Charindex('" & Me.ComboBox4.Text.Trim & "',Isnull(b.CompleteStatus,''),0)>0 " + vbNewLine
                End If
            End If

            If Me.TextBox1.Text.Trim <> "" Then
                sql &= " and convert(int,substring(ReceiveCallTime,1,case when charindex(':',ReceiveCallTime)-1 < 0 then 0 else charindex(':',ReceiveCallTime)-1 end))>=" & Val(Me.TextBox1.Text.Trim) & "   " + vbNewLine
            End If
            If Me.TextBox2.Text.Trim <> "" Then
                sql &= " and convert(int,substring(ReceiveCallTime,1,case when charindex(':',ReceiveCallTime)-1 < 0 then 0 else charindex(':',ReceiveCallTime)-1 end))<= " & Val(Me.TextBox2.Text.Trim) & "   " + vbNewLine
            End If

            ''負責人
            If Me.TextBox4.Text.Trim <> "" Then
                sql &= "   and Isnull(LastCallBackPsn,ListenPsn)='" & Me.TextBox4.Text.Trim & "' " + vbNewLine
            End If

        End If

        Return sql
    End Function

    Private Sub ExecFind_2(ByVal whereExp As String, ByVal whereExp2 As String)
        Me.Cursor = Cursors.WaitCursor
        Dim sql As String = ""

        If Me.cbxDeliveryType.Text.Trim.IndexOf("行送") > 0 Then

            sql &= " select ReceiveCallDate,SupplierName,PurchaseDate,fSaleOrdeNo,GoodsName,GoodsDescript,DGDelivery=DeliveryAddress,ListenPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,Contents,"
            sql &= " LastCallBackRemark"
            sql &= " from CS_ReceiveInformation a  " & whereExp & "       and Isnull(fMulti,0)= 0    " & vbCrLf
            sql &= " union all " & vbCrLf

            sql &= "  select ReceiveCallDate,b.SupplierName,b.PurchaseDate,b.fSaleOrdeNo,b.GoodsName,b.GoodsDescript,DGDelivery=b.DeliveryAddress,ListenPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,Contents=case when Isnull(Contents2,'')<>'' then Contents2 when Isnull(Remark2,'')<>'' then Remark2  else Contents end, LastCallBackRemark"
            sql &= "  from CS_ReceiveInformation a"
            sql &= "  inner join CS_ReceiveSaleInfo b on  a.BoNum=b.BoNum   " & whereExp2 & vbCrLf

        Else
            If Me.fLoadBool = False Then
                sql &= "select ReceiveCallDate,fSaleOrdeNo,DSCTruck,DeliveryAddress,ListenPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,LastCallBackPsn=fEmployeeName,Contents," & vbCrLf
                sql &= "LastCallBackRemark, LastCallBackDate" & vbCrLf
                sql &= "from CS_ReceiveInformation  a  " & whereExp & " "
            Else
                sql &= "select ReceiveCallDate,fSaleOrdeNo,DSCTruck,DeliveryAddress,ListenPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,LastCallBackPsn=fEmployeeName,Contents," & vbCrLf
                sql &= "LastCallBackRemark, LastCallBackDate" & vbCrLf
                sql &= "from CS_ReceiveInformation " & whereExp & "        and Isnull(fMulti,0)= 0     " & vbCrLf
                sql &= " union all" & vbCrLf
                'sql &= "select ReceiveCallDate,fSaleOrdeNo=dbo.GetfSaleOrdeNo(Remark,'" & Me.cbxDeliveryType.Text.Trim & "'),ListenPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,LastCallBackPsn,Contents," & vbCrLf
                'sql &= "LastCallBackRemark, LastCallBackDate" & vbCrLf
                'sql &= "from CS_ReceiveInformation " & whereExp2 & "       and Charindex('@',Remark)>0  and charindex('" & Me.cbxDeliveryType.Text.Trim & "',Remark)>0" & vbCrLf
                sql &= " select ReceiveCallDate,b.fSaleOrdeNo,case when isnull(b.DSCTruck,'')='' then a.DSCTruck else b.DSCTruck end as DSCTruck,DeliveryAddress=Case when Isnull(b.DeliveryAddress,'')='' then a.DeliveryAddress else b.DeliveryAddress end,ListenPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,LastCallBackPsn=case when Isnull(fEmployeeName2,'')='' then fEmployeeName else fEmployeeName2 end,Contents=case when Isnull(Contents2,'')<>'' then Contents2 when Isnull(Remark2,'')<>'' then Remark2  else Contents end,"
                sql &= " LastCallBackRemark, LastCallBackDate"
                sql &= " from CS_ReceiveInformation  a inner join  CS_ReceiveSaleInfo b on a.BoNum=b.BoNum    " & whereExp2
            End If
        End If
        Try
            Dim dtab As New DataTable
            dtab = Exec(sql, E_ExecType.DataTable)
            Me.DataGridView1.Columns.Clear()
            Me.DataGridView1.DataSource = dtab
            If Me.cbxDeliveryType.Text.Trim.IndexOf("行送") > 0 Then
                BindDataGridViewCaption(Me.DataGridView1, Me.ReturnCaption(3))
                SetDataGridWidth_2_1()
            Else
                BindDataGridViewCaption(Me.DataGridView1, Me.ReturnCaption(2))
                SetDataGridWidth_2()
            End If
            FullFormatDataGridView(Me.DataGridView1, False, False)
            Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
            If Me.fLoadBool = True Then
                SetButton(ButtonType.Browse, False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try

    End Sub

    Private Sub SetDataGridWidth_2()
        Me.DataGridView1.Columns("ReceiveCallDate").Width = 70
        Me.DataGridView1.Columns("fSaleOrdeNo").Width = 100
        Me.DataGridView1.Columns("DSCTruck").Width = 70
        Me.DataGridView1.Columns("DeliveryAddress").Width = 250
        Me.DataGridView1.Columns("ListenPsn").Width = 60
        Me.DataGridView1.Columns("LastCallBackPsn").Width = 60
        Me.DataGridView1.Columns("Contents").Width = 300
        Me.DataGridView1.Columns("LastCallBackRemark").Width = 250
        Me.DataGridView1.Columns("LastCallBackDate").Width = 70

    End Sub

    Private Sub SetDataGridWidth_2_1()
        Me.DataGridView1.Columns("ReceiveCallDate").Width = 70
        Me.DataGridView1.Columns("SupplierName").Width = 50
        Me.DataGridView1.Columns("PurchaseDate").Width = 70
        Me.DataGridView1.Columns("fSaleOrdeNo").Width = 80

        Me.DataGridView1.Columns("GoodsName").Width = 80
        Me.DataGridView1.Columns("DGDelivery").Width = 200
        Me.DataGridView1.Columns("ListenPsn").Width = 60
        Me.DataGridView1.Columns("Contents").Width = 200
        Me.DataGridView1.Columns("LastCallBackRemark").Width = 200

    End Sub

#End Region

#Region "ExecFind_3"

    Private Function ReturnWhere_3()
        Dim sql As String = ""

        If Me.fLoadBool = False Then
            sql &= "  where 1<>1 "
        Else
            sql &= " where 1=1 and not exists (select * from CS_CusterServiceTypes where fCode=ItemParentNo)  "

            sql &= "   and ReceiveCallDate>='" & Format(Me.dtp_start_3.Value, "yyyy-MM-dd") & "' "
            sql &= "   and ReceiveCallDate<='" & Format(Me.dtp_end_3.Value, "yyyy-MM-dd") & "' "

            If Not ComboBox3.SelectedValue Is Nothing AndAlso Me.ComboBox3.Text.Trim <> "" Then
                sql &= "  and (Isnull(ItemNo,'') = '" & Me.ComboBox3.SelectedValue & "" & "'   or (Isnull(ItemNo,'')='1101'  and Isnull(ItemNo2,'')='" & Me.ComboBox3.SelectedValue & "" & "'  ))"
            Else
                sql &= "  and (Isnull(ItemNo,'') In ('0205','0206','0207','0208')  or (Isnull(ItemNo,'')='1101'  and Isnull(ItemNo2,'')  in ('0205','0206','0207','0208')  ))"
            End If

            If cbxAgentCode.Text.Trim <> "" Then
                If [Enum].IsDefined(GetType(ModEnum.E_AgentType), Me.cbxAgentCode.Text.Trim) = True Then
                    Select Case Me.cbxAgentCode.Text.Trim
                        Case ModEnum.E_AgentType.A貨.ToString
                            sql &= "  and Isnull(SupplierName,'')='A貨' "
                        Case ModEnum.E_AgentType.行貨.ToString
                            sql &= "  and Isnull(SupplierName,'')<>'A貨'  and Isnull(SupplierName,'')<>'' "
                        Case ModEnum.E_AgentType.未填.ToString
                            sql &= "  and Isnull(SupplierName,'')=''  "
                    End Select
                Else
                    sql &= "  and SupplierName='" & Me.cbxAgentCode.Text.Trim & "' "
                End If
            End If

            If Me.ComboBox8.Text.Trim <> "" Then
                If Me.ComboBox8.Text.Trim = ModEnum.CallBackStatus.已回複.ToString Then
                    sql &= "  and Isnull(CallBackStatus,0)=1  "
                Else
                    sql &= "  and Isnull(CallBackStatus,0)=0  "
                End If
            End If

            If Me.ComboBox7.Text.Trim <> "" Then
                If Me.ComboBox7.Text.Trim = CompleteStatus.已完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0)"
                ElseIf Me.ComboBox7.Text.Trim = CompleteStatus.未完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0) "
                ElseIf Me.ComboBox7.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
                    sql &= "  and (Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0)"
                Else
                    sql &= "  and (Charindex('" & Me.ComboBox7.Text.Trim & "',Isnull(LastCompleteRemark,''),0)>0 or  charindex('" & Me.ComboBox7.Text.Trim & "',Isnull(CompleteRemark,''),0)>0) "
                End If
            End If

            If Me.txtListenPsn_3.Text.Trim <> "" AndAlso Me.txtLastCallBackPsn_3.Text.Trim <> "" Then
                sql &= "  and (ListenPsn='" & Me.txtListenPsn_3.Text.Trim & "'   or BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn_3.Text.Trim & "'))"
            Else
                If Me.txtListenPsn_3.Text <> "" Then
                    sql &= "  and ListenPsn='" & Me.txtListenPsn_3.Text.Trim & "' "
                End If
                If Me.txtLastCallBackPsn_3.Text.Trim <> "" Then
                    'sql &= " and LastCallBackPsn='" & Me.txtLastCallBackPsn_1.Text.Trim & "'"
                    sql &= "   and BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn_3.Text.Trim & "')"
                End If
            End If
            ''負責人
            If Me.TextBox5.Text.Trim <> "" Then
                sql &= "   and Isnull(LastCallBackPsn,ListenPsn)='" & Me.TextBox5.Text.Trim & "' "
            End If

            sql &= "    and Isnull(fMulti,0)= 0   "
        End If
        Return sql
    End Function

    Private Function ReturnWhere_3_2()
        Dim sql As String = ""

        If Me.fLoadBool = False Then
            sql &= "  where 1<>1 "
        Else
            sql &= " where 1=1 and not exists (select * from CS_CusterServiceTypes where fCode=b.ReceiveCode)  "

            sql &= "   and ReceiveCallDate>='" & Format(Me.dtp_start_3.Value, "yyyy-MM-dd") & "' "
            sql &= "   and ReceiveCallDate<='" & Format(Me.dtp_end_3.Value, "yyyy-MM-dd") & "' "

            If Not ComboBox3.SelectedValue Is Nothing AndAlso Me.ComboBox3.Text.Trim <> "" Then
                'sql &= "  and (Isnull(ItemNo,'') = '" & Me.ComboBox3.SelectedValue & "" & "'   or (Isnull(ItemNo,'')='1101'  and Isnull(ItemNo2,'')='" & Me.ComboBox3.SelectedValue & "" & "'  ))"
                'sql &= " and  (a.ItemParentNo='02' or (a.ItemParentNo='11' and ItemNo='1101' and ItemParentNo2='02'))" & vbCrLf
                sql &= " and  Isnull(b.ReceiveCode,'')='" & Me.ComboBox3.SelectedValue & "" & "' " & vbCrLf
            Else
                'sql &= "  and (Isnull(ItemNo,'') In ('0205','0206','0207','0208')  or (Isnull(ItemNo,'')='1101'  and Isnull(ItemNo2,'')  in ('0205','0206','0207','0208')  ))"
                'sql &= "  and  (a.ItemParentNo='02' or (a.ItemParentNo='11' and ItemNo='1101' and ItemParentNo2='02'))" & vbCrLf
                sql &= "  and (Isnull(b.ReceiveCode,'')  in ('0205','0206','0207','0208'))    " & vbCrLf
            End If

            If cbxAgentCode.Text.Trim <> "" Then
                If [Enum].IsDefined(GetType(ModEnum.E_AgentType), Me.cbxAgentCode.Text.Trim) = True Then
                    Select Case Me.cbxAgentCode.Text.Trim
                        Case ModEnum.E_AgentType.A貨.ToString
                            sql &= "  and Isnull(b.SupplierName,'')='A貨' "
                        Case ModEnum.E_AgentType.行貨.ToString
                            sql &= "  and Isnull(b.SupplierName,'')<>'A貨'  and Isnull(b.SupplierName,'')<>'' "
                        Case ModEnum.E_AgentType.未填.ToString
                            sql &= "  and Isnull(b.SupplierName,'')=''  "
                    End Select
                Else
                    sql &= "  and b.SupplierName='" & Me.cbxAgentCode.Text.Trim & "' "
                End If
            End If

            If Me.ComboBox8.Text.Trim <> "" Then
                If Me.ComboBox8.Text.Trim = ModEnum.CallBackStatus.已回複.ToString Then
                    sql &= "  and Isnull(CallBackStatus,0)=1  "
                Else
                    sql &= "  and Isnull(CallBackStatus,0)=0  "
                End If
            End If

            'If Me.ComboBox7.Text.Trim <> "" Then
            '    If Me.ComboBox7.Text.Trim = CompleteStatus.已完成.ToString() Then
            '        sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0)"
            '    ElseIf Me.ComboBox7.Text.Trim = CompleteStatus.未完成.ToString() Then
            '        sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0) "
            '    ElseIf Me.ComboBox7.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
            '        sql &= "  and (Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0)"
            '    Else
            '        sql &= "  and (Charindex('" & Me.ComboBox7.Text.Trim & "',Isnull(LastCompleteRemark,''),0)>0 or  charindex('" & Me.ComboBox7.Text.Trim & "',Isnull(CompleteRemark,''),0)>0) "
            '    End If
            'End If

            If Me.ComboBox7.Text.Trim <> "" Then
                If Me.ComboBox7.Text.Trim = CompleteStatus.已完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(b.CompleteStatus,''),0)>0 or Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0) "
                ElseIf Me.ComboBox7.Text.Trim = CompleteStatus.未完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(b.CompleteStatus,''),0)<=0  and Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0 )"
                ElseIf Me.ComboBox7.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
                    sql &= "  and (Charindex('跟進',Isnull(b.CompleteStatus,''),0)>0 or Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0 and Charindex('ok',Isnull(b.CompleteStatus,''),0)<=0) "
                Else
                    sql &= "  and Charindex('" & Me.ComboBox7.Text.Trim & "',Isnull(b.CompleteStatus,''),0)>0 "
                End If
            End If

            If Me.txtListenPsn_3.Text.Trim <> "" AndAlso Me.txtLastCallBackPsn_3.Text.Trim <> "" Then
                sql &= "  and (ListenPsn='" & Me.txtListenPsn_3.Text.Trim & "'   or a.BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn_3.Text.Trim & "'))"
            Else
                If Me.txtListenPsn_3.Text <> "" Then
                    sql &= "  and ListenPsn='" & Me.txtListenPsn_3.Text.Trim & "' "
                End If
                If Me.txtLastCallBackPsn_3.Text.Trim <> "" Then
                    'sql &= " and LastCallBackPsn='" & Me.txtLastCallBackPsn_1.Text.Trim & "'"
                    sql &= "   and a.BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.txtLastCallBackPsn_3.Text.Trim & "')"
                End If
            End If

            ''負責人
            If Me.TextBox5.Text.Trim <> "" Then
                sql &= "   and Isnull(LastCallBackPsn,ListenPsn)='" & Me.TextBox5.Text.Trim & "' "
            End If

        End If
        Return sql
    End Function

    Private Sub ExecFind_3(ByVal whereExp As String, ByVal whereExp2 As String)
        Dim sql As String = ""

        'sql &= "  declare @Tab nvarchar(50)" & vbCrLf
        'sql &= " select Id=Identity(int,1,1), ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),fSaleOrdeNo,SupplierName=Case when Isnull(SupplierName,'')='' and Isnull(fCode,'') in ('0206','0208') then 'A貨' else SupplierName end ," & vbCrLf
        'sql &= " fCode,fGoodsType=Case when Isnull(fCode,'') in ('0205','0206') then '家俬' when Isnull(fCode,'') in ('0207','0208') then  '電器' else '' end," & vbCrLf
        'sql &= " ListenPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End, GoodsName, GoodsDescript  " & vbCrLf
        'sql &= "   into  [#@Tab]   from  dbo.CS_ReceiveInformation  a " & vbCrLf
        'sql &= " left join dbo.CS_CusterServiceType b on b.fCode=case when Isnull(a.ItemNo,'')='1101' then Isnull(ItemNo2,'') else  Isnull(ItemNo,'') end  " & whereExp & vbCrLf
        'sql &= "  select * from [#@Tab]" & vbCrLf
        'sql &= " Drop table [#@Tab] " & vbCrLf
        sql &= "  declare @Tab nvarchar(50)" & vbCrLf


        sql &= " select  Id=Identity(int,1,1) ,*  into [#@Tab]  from (" & vbCrLf
        sql &= " select  ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),fSaleOrdeNo,SupplierName=Case when Isnull(SupplierName,'')='' and Isnull(fCode,'') in ('0206','0208') then 'A貨' else SupplierName end ," & vbCrLf
        sql &= " fCode,fGoodsType=Case when Isnull(fCode,'') in ('0205','0206') then '家俬' when Isnull(fCode,'') in ('0207','0208') then  '電器' else '' end," & vbCrLf
        sql &= " ListenPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End, GoodsName, GoodsDescript  " & vbCrLf
        sql &= " from  dbo.CS_ReceiveInformation  a " & vbCrLf
        sql &= " left join dbo.CS_CusterServiceType b on b.fCode=case when Isnull(a.ItemNo,'')='1101' then Isnull(ItemNo2,'') else  Isnull(ItemNo,'') end   " & vbCrLf
        sql &= " " & whereExp & vbCrLf
        sql &= " union   all" & vbCrLf
        sql &= " select  ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),b.fSaleOrdeNo,SupplierName=Case when Isnull(b.SupplierName,'')='' and Isnull(fCode,'') in ('0206','0208') then 'A貨' else b.SupplierName end ," & vbCrLf
        sql &= " fCode,fGoodsType=Case when Isnull(fCode,'') in ('0205','0206') then '家俬' when Isnull(fCode,'') in ('0207','0208') then  '電器' else '' end," & vbCrLf
        sql &= " ListenPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End, b.GoodsName, b.GoodsDescript  " & vbCrLf
        sql &= " from  dbo.CS_ReceiveInformation  a " & vbCrLf
        sql &= " inner join dbo.CS_ReceiveSaleInfo b on a.BoNum=b.BoNum" & vbCrLf
        sql &= " left join dbo.CS_CusterServiceType c on c.fCode=case when Isnull(a.ItemNo,'')='1101' then Isnull(ItemNo2,'') else  Isnull(ItemNo,'') end    " & vbCrLf
        sql &= " " & whereExp2 & vbCrLf
        sql &= "  ) a " & vbCrLf
        sql &= "  select * from [#@Tab]" & vbCrLf
        sql &= "  Drop table      [#@Tab] " & vbCrLf

        Try
            Dim Dtab As New DataTable

            Dtab = Exec(sql, E_ExecType.DataTable)
            Me.dgv_3.DataSource = Dtab
            BindDataGridViewCaption(Me.dgv_3, Me.ReturnCaption(4))
            FullFormatDataGridView(Me.dgv_3, False, False)
            Me.dgv_3.SelectionMode = DataGridViewSelectionMode.CellSelect
            SetDataGridWidth_4()
            If Me.fLoadBool = True Then
                SetButton(ButtonType.Browse, False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub SetDataGridWidth_4()
        Me.dgv_3.Columns("Id").Width = 60
        Me.dgv_3.Columns("ReceiveCallDate").Width = 80
        Me.dgv_3.Columns("fSaleOrdeNo").Width = 120
        Me.dgv_3.Columns("SupplierName").Width = 80
        Me.dgv_3.Columns("fGoodsType").Width = 60
        Me.dgv_3.Columns("ListenPsn").Width = 80
        Me.dgv_3.Columns("GoodsDescript").Width = 300
    End Sub

#End Region

#Region "Find_4"

    Private Function ReturnWhere_4() As String
        Dim sql As String = ""

        If Me.fLoadBool = False Then
            sql &= "  where 1<>1 "
        Else
            sql &= "   where 1=1 and not exists (select * from CS_CusterServiceTypes where fCode=ItemParentNo)  "

            If Not Me.TreeView2.SelectedNode Is Nothing Then
                Dim Node As TreeNode = Me.TreeView2.SelectedNode
                Dim key As String = Node.Tag & ""
                Dim parentKey As String = ""
                If Not Node.Parent Is Nothing Then
                    parentKey = Node.Parent.Tag & ""
                End If
                Select Case Node.Level
                    Case 0
                        sql &= " and Isnull(ItemParentNo,'')='12' " & vbCrLf
                    Case 1

                        If key.Trim = "01" Then
                            sql &= " and Isnull(ItemParentNo,'')='12' and Isnull(ItemNo,'') = '1201' " & vbCrLf
                        ElseIf key.Trim = "02" Then
                            sql &= " and Isnull(ItemParentNo,'')='12' and Isnull(ItemNo,'') = '1202' " & vbCrLf
                        Else
                            sql &= " and Isnull(ItemParentNo,'')='12' and Isnull(ItemNo,'')='1203' " & vbCrLf
                        End If

                    Case 2

                        If parentKey.Trim = "01" Then
                            sql &= " and Isnull(ItemParentNo,'')='12' and  Isnull(ItemNo,'')='1201'  and Isnull(ItemName,'')='" & key & "' " & vbCrLf
                        ElseIf parentKey.Trim = "02" Then
                            sql &= " and Isnull(ItemParentNo,'')='12' and  Isnull(ItemNo,'')='1202'  and Isnull(ItemName,'')='" & key & "' " & vbCrLf
                        Else
                            sql &= " and Isnull(ItemParentNo,'')='12' and  Isnull(ItemNo,'')='1203'  and Isnull(ItemName,'')='" & key & "' " & vbCrLf
                        End If

                End Select
            Else
                sql &= " and (Isnull(ItemParentNo,'')='05'  or (Isnull(ItemParentNo,'')='11' and Isnull(ItemNo,'')='1101'  and Isnull(ItemParentNo2,'')='05'  )) " & vbCrLf
            End If

            sql &= "  and ReceiveCallDate>='" & Format(Me.dtp_star_4.Value, "yyyy-MM-dd") & "'"
            sql &= "  and ReceiveCallDate<='" & Format(Me.dtp_end_4.Value, "yyyy-MM-dd") & "'"

            If Me.ComboBox11.Text.Trim <> "" Then
                sql &= " and Isnull(ComplainLevel,'一般')='" & Me.ComboBox11.Text & "'"
            End If

            If Me.ComboBox12.Text.Trim <> "" Then
                If [Enum].IsDefined(GetType(ModEnum.E_AgentType), Me.ComboBox12.Text.Trim) = True Then
                    Select Case Me.ComboBox12.Text.Trim
                        Case ModEnum.E_AgentType.A貨.ToString
                            sql &= "  and Isnull(a.SupplierName,'')='A貨' "
                        Case ModEnum.E_AgentType.行貨.ToString
                            sql &= "  and Isnull(a.SupplierName,'')<>'A貨'  and Isnull(a.SupplierName,'')<>'' "
                        Case ModEnum.E_AgentType.未填.ToString
                            sql &= "  and Isnull(a.SupplierName,'')=''  "
                    End Select
                Else
                    sql &= "  and Isnull(a.SupplierName,'')='" & Me.ComboBox12.Text.Trim & "' "
                End If

            End If

            If Me.ComboBox10.Text.Trim <> "" Then
                If Me.ComboBox10.Text.Trim = ModEnum.CallBackStatus.已回複.ToString Then
                    sql &= "  and Isnull(CallBackStatus,0)=1  " & vbCrLf
                Else
                    sql &= "  and Isnull(CallBackStatus,0)=0  " & vbCrLf
                End If
            End If

            If Me.ComboBox9.Text.Trim <> "" Then
                If Me.ComboBox9.Text.Trim = CompleteStatus.已完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0)" & vbCrLf
                ElseIf Me.ComboBox9.Text.Trim = CompleteStatus.未完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0) " & vbCrLf
                ElseIf Me.ComboBox9.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
                    sql &= "  and (Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0)" & vbCrLf
                Else
                    sql &= "  and (Charindex('" & Me.ComboBox9.Text.Trim & "',Isnull(LastCompleteRemark,''),0)>0 or  charindex('" & Me.ComboBox9.Text.Trim & "',Isnull(CompleteRemark,''),0)>0) " & vbCrLf
                End If
            End If

            If Me.TextBox15.Text.Trim <> "" AndAlso Me.TextBox13.Text.Trim <> "" Then
                sql &= "  and (ListenPsn='" & Me.TextBox15.Text.Trim & "'   or a.BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.TextBox13.Text.Trim & "'))"
            Else
                If Me.TextBox15.Text <> "" Then
                    sql &= "  and ListenPsn='" & Me.TextBox15.Text.Trim & "' "
                End If
                If Me.TextBox13.Text.Trim <> "" Then
                    'sql &= " and LastCallBackPsn='" & Me.txtLastCallBackPsn_1.Text.Trim & "'"
                    sql &= "   and a.BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.TextBox13.Text.Trim & "')"
                End If
            End If

            If Me.TextBox6.Text.Trim <> "" Then
                sql &= " and convert(int,substring(ReceiveCallTime,1,charindex(':',ReceiveCallTime)-1))>=" & Val(Me.TextBox6.Text.Trim) & "   "
            End If

            If Me.TextBox7.Text.Trim <> "" Then

                sql &= " and convert(int,substring(ReceiveCallTime,1,charindex(':',ReceiveCallTime)-1))<=" & Val(Me.TextBox7.Text.Trim) & "   "
            End If


            ''負責人
            If Me.TextBox12.Text.Trim <> "" Then
                sql &= "   and Isnull(LastCallBackPsn,ListenPsn)='" & Me.TextBox12.Text.Trim & "' "
            End If

            ''  sql &= "  and Isnull(fMulti,0)=0 " & vbCrLf
        End If

        Return sql
    End Function

    Private Function ReturnWhere_4_2() As String
        Dim sql As String = ""

        If Me.fLoadBool = False Then
            sql &= "  where 1<>1 "
        Else
            sql &= "   where 1=1 and not exists (select * from CS_CusterServiceTypes where fCode=b.ReceiveCode)  "

            If Not Me.TreeView2.SelectedNode Is Nothing Then
                Dim Node As TreeNode = Me.TreeView2.SelectedNode
                Dim key As String = Node.Tag & ""
                Dim parentKey As String = ""
                If Not Node.Parent Is Nothing Then
                    parentKey = Node.Parent.Tag & ""
                End If
                Select Case Node.Level
                    Case 0
                        sql &= " and Isnull(b.ReceiveCode,'') in (select fCode from CS_CusterServiceType where fParentCode='12')" & vbCrLf
                    Case 1

                        If key.Trim = "01" Then
                            sql &= " and  Isnull(b.ReceiveCode,'')='1201' " & vbCrLf
                        ElseIf key.Trim = "02" Then
                            sql &= " and  Isnull(b.ReceiveCode,'')='1202' " & vbCrLf
                        Else
                            sql &= " and  Isnull(b.ReceiveCode,'')='1203' " & vbCrLf
                        End If

                    Case 2

                        'If parentKey.Trim = "01" Then
                        sql &= " and Isnull(b.ReceiveCode,'')='" & key & "' "
                        '  End If

                End Select
            Else
                sql &= "   and  Isnull(b.ReceiveCode,'') in (select fCode from CS_CusterServiceType where fParentCode='05')" & vbCrLf
            End If

            sql &= "  and ReceiveCallDate>='" & Format(Me.dtp_star_4.Value, "yyyy-MM-dd") & "'"
            sql &= "  and ReceiveCallDate<='" & Format(Me.dtp_end_4.Value, "yyyy-MM-dd") & "'"

            If Me.ComboBox11.Text.Trim <> "" Then
                sql &= " and Isnull(ComplainLevel,'一般')='" & Me.ComboBox11.Text & "'"
            End If

            If Me.ComboBox12.Text.Trim <> "" Then
                If [Enum].IsDefined(GetType(ModEnum.E_AgentType), Me.ComboBox12.Text.Trim) = True Then
                    Select Case Me.ComboBox12.Text.Trim
                        Case ModEnum.E_AgentType.A貨.ToString
                            sql &= "  and Isnull(b.SupplierName,'')='A貨' "
                        Case ModEnum.E_AgentType.行貨.ToString
                            sql &= "  and Isnull(b.SupplierName,'')<>'A貨' and Isnull(b.SupplierName,'')<>'' "
                        Case ModEnum.E_AgentType.未填.ToString
                            sql &= "  and Isnull(b.SupplierName,'')='' "
                    End Select
                Else
                    sql &= "  and Isnull(b.SupplierName,'')='" & Me.ComboBox12.Text.Trim & "' "
                End If

            End If

            If Me.ComboBox10.Text.Trim <> "" Then
                If Me.ComboBox10.Text.Trim = ModEnum.CallBackStatus.已回複.ToString Then
                    sql &= "  and Isnull(CallBackStatus,0)=1 "
                Else
                    sql &= "  and Isnull(CallBackStatus,0)=0 "
                End If
            End If

            'If Me.ComboBox6.Text.Trim <> "" Then
            '    If Me.ComboBox6.Text.Trim = CompleteStatus.已完成.ToString() Then
            '        sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0)" & vbCrLf
            '    ElseIf Me.ComboBox6.Text.Trim = CompleteStatus.未完成.ToString() Then
            '        sql &= "  and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0) " & vbCrLf
            '    ElseIf Me.ComboBox6.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
            '        sql &= "  and (Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0)" & vbCrLf
            '    Else
            '        sql &= "  and (Charindex('" & Me.ComboBox6.Text.Trim & "',Isnull(LastCompleteRemark,''),0)>0 or  charindex('" & Me.ComboBox6.Text.Trim & "',Isnull(CompleteRemark,''),0)>0) " & vbCrLf
            '    End If
            'End If

            If Me.ComboBox9.Text.Trim <> "" Then
                If Me.ComboBox9.Text.Trim = CompleteStatus.已完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(b.CompleteStatus,''),0)>0 or Charindex('ok',Isnull(LastCompleteRemark,''),0)>0 or charindex('ok',Isnull(CompleteRemark,''),0)>0)"
                ElseIf Me.ComboBox9.Text.Trim = CompleteStatus.未完成.ToString() Then
                    sql &= "  and (Charindex('ok',Isnull(b.CompleteStatus,''),0)<=0  and Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0)"
                ElseIf Me.ComboBox9.Text.Trim = CompleteStatus.跟進.ToString.Trim Then
                    sql &= "  and (Charindex('跟進',Isnull(b.CompleteStatus,''),0)>0 or Charindex('跟進',Isnull(LastCompleteRemark,''),0)>0 or charindex('跟進',Isnull(CompleteRemark,''),0)>0) and (Charindex('ok',Isnull(LastCompleteRemark,''),0)<=0 and  charindex('ok',Isnull(CompleteRemark,''),0)<=0 and Charindex('ok',Isnull(b.CompleteStatus,''),0)<=0 )" & vbCrLf
                Else
                    sql &= "  and Charindex('" & Me.ComboBox9.Text.Trim & "',Isnull(b.CompleteStatus,''),0)>0  "
                End If
            End If

            If Me.TextBox15.Text.Trim <> "" AndAlso Me.TextBox13.Text.Trim <> "" Then
                sql &= "  and (ListenPsn='" & Me.TextBox15.Text.Trim & "'   or a.BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.TextBox13.Text.Trim & "'))"
            Else
                If Me.TextBox15.Text <> "" Then
                    sql &= "  and ListenPsn='" & Me.TextBox15.Text.Trim & "' "
                End If
                If Me.TextBox13.Text.Trim <> "" Then
                    'sql &= " and LastCallBackPsn='" & Me.txtLastCallBackPsn_1.Text.Trim & "'"
                    sql &= "   and a.BoNum in (select  Distinct BoNum from CS_CallBackInformation where CallBackPsn = '" & Me.TextBox13.Text.Trim & "')"
                End If
            End If

            If Me.TextBox6.Text.Trim <> "" Then
                sql &= " and convert(int,substring(ReceiveCallTime,1,charindex(':',ReceiveCallTime)-1))>=" & Val(Me.TextBox6.Text.Trim) & "   "
            End If

            If Me.TextBox7.Text.Trim <> "" Then

                sql &= " and convert(int,substring(ReceiveCallTime,1,charindex(':',ReceiveCallTime)-1))<=" & Val(Me.TextBox7.Text.Trim) & "   "
            End If


            ''負責人
            If Me.TextBox12.Text.Trim <> "" Then
                sql &= "   and Isnull(LastCallBackPsn,ListenPsn)='" & Me.TextBox12.Text.Trim & "' "
            End If

        End If

        Return sql
    End Function

    Public Sub ExecFind_4(ByVal whereExp As String, ByVal whereExp2 As String)

        Me.Cursor = Cursors.WaitCursor

        Dim sql As String = ""
        sql &= " select distinct ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),ReceiveCallTime,fSaleOrdeNo,PurchaseDate=Replace(PurchaseDate,'-','/'),SupplierName,GoodsName,a.enname,GoodsDescript," & vbCrLf
        sql &= " Contents,fEmployeeName,DSCTruck,DGKD,DGDelivery,CustomerName,ContactPhone," & vbCrLf
        sql &= " LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,LastCallBackRemark=Case When Isnull(LastCallBackRemark,'')='' then Remark else LastCallBackRemark End,CompleteRemark=Case when Isnull(LastCompleteRemark,'')=''   then CompleteRemark else LastCompleteRemark  end ,LastCallBackDate=Case when  (charindex('ok',CompleteRemark,0)>0 or charindex('ok',LastCompleteRemark,0)>0) then (case when Isnull(LastCallBackDate,'')='' then Replace(ReceiveCallDate,'-','/')  else Replace(LastCallBackDate,'-','/')   end) else Null end    from CS_ReceiveInformation  a " & whereExp & vbCrLf
        sql &= " Union all" & vbCrLf

        sql &= " select  ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),ReceiveCallTime,b.fSaleOrdeNo,PurchaseDate=Replace(b.PurchaseDate,'-','/'),b.SupplierName,b.GoodsName,a.enname,b.GoodsDescript," & vbCrLf
        sql &= " Contents=case when Isnull(Contents2,'')<>'' then Contents2 when Isnull(Remark2,'')<>'' then Remark2  else Contents end  ,fEmployeeName,case when isnull(b.DSCTruck,'')='' then a.DSCTruck else b.DSCTruck end as DSCTruck,case when isnull(b.DGKD,'')='' then a.DGKD  else b.DGKD end as DGKD,case when isnull(b.DGDelivery,'')='' then a.DGDelivery else b.DGDelivery end as DGDelivery,CustomerName,ContactPhone," & vbCrLf
        sql &= " LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End," & vbCrLf
        sql &= " LastCallBackRemark=Case When Isnull(LastCallBackRemark,'')='' then Remark else LastCallBackRemark End," & vbCrLf
        sql &= " CompleteRemark=Case when Isnull(LastCompleteRemark,'')=''   then CompleteRemark else LastCompleteRemark  " & vbCrLf
        sql &= " end ,LastCallBackDate=Case when  (charindex('ok',CompleteRemark,0)>0 or charindex('ok',LastCompleteRemark,0)>0) then (case when Isnull(LastCallBackDate,'')='' then Replace(ReceiveCallDate,'-','/')  else Replace(LastCallBackDate,'-','/')   end) else Null end    " & vbCrLf
        sql &= " from CS_ReceiveInformation  a" & vbCrLf
        sql &= " inner join CS_ReceiveSaleInfo b on a.BoNum=b.BoNum  " & whereExp2 & vbCrLf

        Try
            Dim dtab As New DataTable
            dtab = Exec(sql, E_ExecType.DataTable)
            Me.dgv_4.DataSource = dtab
            BindDataGridViewCaption(Me.dgv_4, Me.ReturnCaption(1))
            FullFormatDataGridView(Me.dgv_4, False, False)
            Me.dgv_4.SelectionMode = DataGridViewSelectionMode.CellSelect
            SetDataGridWidth_1()
            If Me.fLoadBool = True Then
                SetButton(ButtonType.Browse, False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    ''' <summary>
    ''' 華方來電\落維修單，優質來電\落維修單 需要導出的內容
    ''' </summary>
    ''' <param name="whereExp"></param>
    ''' <param name="whereExp2"></param>
    ''' <remarks></remarks>
    ''' 

    Public Sub ExecFind_4_1(ByVal whereExp As String, ByVal whereExp2 As String)

        Me.Cursor = Cursors.WaitCursor

        Dim sql As String = ""
        sql &= " ---華方來電\落維修單，優質來電\落維修單 需要導出的內容 " & vbCrLf
        sql &= " select distinct ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),fSaleOrdeNo,'' as fWriteNo,PurchaseDate=Replace(PurchaseDate,'-','/'),'' as BaoYangDate,GoodsName,SupplierName,GoodsDescript," & vbCrLf
        sql &= "    '' as MachineNo,CustomerName,DeliveryAddress,ContactPhone,'' as ContactPhone2,'' as ContactPhone3,''as CanKaoNo, Contents,'' as JianChaFei,'' as FeiYong,LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End, flevel1  from CS_ReceiveInformation  a " & whereExp & vbCrLf
        sql &= " Union all" & vbCrLf

        sql &= " select ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),b.fSaleOrdeNo,'' as fWriteNo,PurchaseDate=Replace(b.PurchaseDate,'-','/'),'' as BaoYangDate,b.GoodsName,b.SupplierName,b.GoodsDescript," & vbCrLf
        sql &= "  '' as MachineNo,CustomerName,b.DeliveryAddress,ContactPhone,'' as ContactPhone2,'' as ContactPhone3, ''as CanKaoNo,Contents=case when Isnull(Contents2,'')<>'' then Contents2 when Isnull(Remark2,'')<>'' then Remark2  else Contents end , '' as JianChaFei,'' as FeiYong,LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,flevel1" & vbCrLf
        sql &= " from CS_ReceiveInformation  a" & vbCrLf
        sql &= " inner join CS_ReceiveSaleInfo b on a.BoNum=b.BoNum  " & whereExp2 & vbCrLf

        Try
            Dim dtab As New DataTable
            dtab = Exec(sql, E_ExecType.DataTable)
            Me.dgv_4.DataSource = dtab
            BindDataGridViewCaption(Me.dgv_4, Me.ReturnCaption(41))
            FullFormatDataGridView(Me.dgv_4, False, False)
            Me.dgv_4.SelectionMode = DataGridViewSelectionMode.CellSelect
            SetDataGridWidth_1()
            If Me.fLoadBool = True Then
                SetButton(ButtonType.Browse, False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    ''' <summary>
    ''' ----華方來電\查詢故障及使用方法,華方來電\查詢購買配件,優質來電\查詢故障及使用方法,優質來電\查詢購買配件
    ''' </summary>
    ''' <param name="whereExp"></param>
    ''' <param name="whereExp2"></param>
    ''' <remarks></remarks>
    ''' 

    Public Sub ExecFind_4_2(ByVal whereExp As String, ByVal whereExp2 As String)

        Me.Cursor = Cursors.WaitCursor

        Dim sql As String = ""
        sql &= " ----華方來電\查詢故障及使用方法,華方來電\查詢購買配件,優質來電\查詢故障及使用方法,優質來電\查詢購買配件" & vbCrLf
        sql &= "   select distinct ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),fSaleOrdeNo,'' as fWriteNo,GoodsName,SupplierName,GoodsDescript," & vbCrLf
        sql &= "  CustomerName,ContactPhone,'' as ContactPhone2,'' as ContactPhone3, Contents,LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End from CS_ReceiveInformation  a " & whereExp & vbCrLf
        sql &= " Union all" & vbCrLf

        sql &= "  select ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),b.fSaleOrdeNo,'' as fWriteNo,b.GoodsName,b.SupplierName,b.GoodsDescript," & vbCrLf
        sql &= "  CustomerName,ContactPhone,'' as ContactPhone2,'' as ContactPhone3,Contents=case when Isnull(Contents2,'')<>'' then Contents2 when Isnull(Remark2,'')<>'' then Remark2  else Contents end ,LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End " & vbCrLf
        sql &= " from CS_ReceiveInformation  a" & vbCrLf
        sql &= " inner join CS_ReceiveSaleInfo b on a.BoNum=b.BoNum  " & whereExp2 & vbCrLf

        Try
            Dim dtab As New DataTable
            dtab = Exec(sql, E_ExecType.DataTable)
            Me.dgv_4.DataSource = dtab
            BindDataGridViewCaption(Me.dgv_4, Me.ReturnCaption(42))
            FullFormatDataGridView(Me.dgv_4, False, False)
            Me.dgv_4.SelectionMode = DataGridViewSelectionMode.CellSelect
            SetDataGridWidth_1()
            If Me.fLoadBool = True Then
                SetButton(ButtonType.Browse, False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    ''' <summary>
    ''' -----華方來電\查詢維修進度,優質來電\查詢維修進度
    ''' </summary>
    ''' <param name="whereExp"></param>
    ''' <param name="whereExp2"></param>
    ''' <remarks></remarks>
    ''' 

    Public Sub ExecFind_4_3(ByVal whereExp As String, ByVal whereExp2 As String)

        Me.Cursor = Cursors.WaitCursor

        Dim sql As String = ""
        sql &= "-----華方來電\查詢維修進度,優質來電\查詢維修進度 " & vbCrLf
        sql &= "  select distinct ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),fSaleOrdeNo,GoodsName,SupplierName,GoodsDescript," & vbCrLf
        sql &= "  Contents,LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End from CS_ReceiveInformation  a " & whereExp & vbCrLf
        sql &= " Union all" & vbCrLf

        sql &= "   select ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),b.fSaleOrdeNo,b.GoodsName,b.SupplierName,b.GoodsDescript," & vbCrLf
        sql &= "  Contents=case when Isnull(Contents2,'')<>'' then Contents2 when Isnull(Remark2,'')<>'' then Remark2  else Contents end ,LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End " & vbCrLf
        sql &= " from CS_ReceiveInformation  a" & vbCrLf
        sql &= " inner join CS_ReceiveSaleInfo b on a.BoNum=b.BoNum  " & whereExp2 & vbCrLf

        Try
            Dim dtab As New DataTable
            dtab = Exec(sql, E_ExecType.DataTable)
            Me.dgv_4.DataSource = dtab
            BindDataGridViewCaption(Me.dgv_4, Me.ReturnCaption(43))
            FullFormatDataGridView(Me.dgv_4, False, False)
            Me.dgv_4.SelectionMode = DataGridViewSelectionMode.CellSelect
            SetDataGridWidth_1()
            If Me.fLoadBool = True Then
                SetButton(ButtonType.Browse, False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

#End Region

#End Region

#Region "SetEnabled"


    ''' <summary>
    ''' 設置按鈕狀態
    ''' </summary>
    ''' <param name="type">操作類型</param>
    ''' <param name="IsFind">是否是流覽</param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try

            Select Case type
                Case ButtonType.Add
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Browse
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = True
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = True
                        .ToolStripFilter.Enabled = True
                        .ToolStripCheck.Enabled = False
                        .ToolStripComplete.Enabled = False
                        .ToolStripUnCheck.Enabled = False
                    End With

                Case ButtonType.Cancel
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Edit
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub



#End Region

#Region "Exprots"


#End Region

#End Region

#Region "Events"

#Region "Form Load"

    Private Sub frm_CSExprot_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.fLoadBool = False
            Initialize()
            Dim whereExp As String = Me.ReturnWhere_1()
            Dim whereExp1_2 As String = Me.ReturnWhere_1_2()
            Me.ExecFind_1(whereExp, whereExp1_2)
            Dim whereExp2 As String = Me.ReturnWhereExp()
            Dim where2_1 As String = ReturnWhereExp_2()
            Me.ExecFind_2(whereExp2, where2_1)
            Dim whereExp3 As String = Me.ReturnWhere_3()
            Dim whereExp3_2 As String = ReturnWhere_3_2()
            Me.ExecFind_3(whereExp3, whereExp3_2)
            Dim whereExp4 As String = Me.ReturnWhere_4()
            Dim whereExp4_2 As String = Me.ReturnWhere_4_2()
            Me.ExecFind_4(whereExp, whereExp4_2)
            Me.fLoadBool = True
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "TreeView"

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

        Dim text As String = e.Node.FullPath
        If e.Node.Level > 0 Then
            text = text.Replace("全部\", "")
        End If
        Me.txtcomplaint.Text = text
        Me.TreeView1.Visible = False
    End Sub

    Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
        Me.Load_TreeNode(e.Node, Me.Treedb)
    End Sub

    Private Sub TreeView1_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeSelect
        If Not Me.TreeView1.SelectedNode Is Nothing Then
            Me.TreeView1.SelectedNode.ForeColor = Color.Black
        End If
        e.Node.ForeColor = Color.Blue
    End Sub

    Private Sub TreeView2_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView2.AfterSelect
        Dim text As String = e.Node.FullPath
        If e.Node.Level > 0 Then
            text = text.Replace("全部\", "")
        End If
        Me.txtcomplaint1.Text = text
        Me.TreeView2.Visible = False
    End Sub

    Private Sub TreeView2_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView2.BeforeExpand
        Me.Load_TreeNode(e.Node, Me.Treedb1)
    End Sub

    Private Sub TreeView2_BeforeSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView2.BeforeSelect
        If Not Me.TreeView2.SelectedNode Is Nothing Then
            Me.TreeView2.SelectedNode.ForeColor = Color.Black
        End If
        e.Node.ForeColor = Color.Blue
    End Sub


#End Region

#Region "Button"

    Private Sub btnTreeView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTreeView.Click
        If Me.TreeView1.Visible = False Then
            Me.TreeView1.Visible = True
        Else
            Me.TreeView1.Visible = False
        End If
    End Sub

    Private Sub btnTreeView2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTreeView2.Click
        If Me.TreeView2.Visible = False Then
            Me.TreeView2.Visible = True
        Else
            Me.TreeView2.Visible = False
        End If
    End Sub

    Private Sub btnFind_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind_1.Click
        Dim whereExp As String = Me.ReturnWhere_1()
        Dim whereExp1_2 As String = Me.ReturnWhere_1_2()
        Me.ExecFind_1(whereExp, whereExp1_2)
    End Sub

    Private Sub btnFind_2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind_2.Click
        Dim whereExp2 As String = Me.ReturnWhereExp()
        Dim where2_1 As String = ReturnWhereExp_2()
        Me.ExecFind_2(whereExp2, where2_1)
    End Sub

    Private Sub btnFind_3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind_3.Click
        Dim whereExp3 As String = Me.ReturnWhere_3()
        Dim whereExp3_2 As String = ReturnWhere_3_2()
        Me.ExecFind_3(whereExp3, whereExp3_2)
    End Sub

    Private Sub btnFind_4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind_4.Click
        Dim whereExp As String = Me.ReturnWhere_4()
        Dim whereExp1_2 As String = Me.ReturnWhere_4_2()

        If Me.txtcomplaint1.Text = "華方來電\落維修單" Or Me.txtcomplaint1.Text = "優質來電\落維修單" Then

            Me.ExecFind_4_1(whereExp, whereExp1_2)
        ElseIf Me.txtcomplaint1.Text = "華方來電\查詢故障及使用方法" Or Me.txtcomplaint1.Text = "華方來電\查詢購買配件" Or Me.txtcomplaint1.Text = "優質來電\查詢故障及使用方法" Or Me.txtcomplaint1.Text = "優質來電\查詢購買配件" Then

            Me.ExecFind_4_2(whereExp, whereExp1_2)
        ElseIf Me.txtcomplaint1.Text = "華方來電\查詢維修進度" Or Me.txtcomplaint1.Text = "優質來電\查詢維修進度" Then

            Me.ExecFind_4_3(whereExp, whereExp1_2)
        Else
            Me.ExecFind_4(whereExp, whereExp1_2)
        End If



    End Sub

#End Region

#Region "DataGridView"

    Private Sub dg_complaint_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dg_complaint.RowPostPaint, dgv_4.RowPostPaint
        Try
            Dim Grid As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(Grid.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
            SBrush = Nothing
        Catch ex As Exception
            MessageBox.Show("DGV_Master_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Try
            Dim Grid As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(Grid.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
            SBrush = Nothing
        Catch ex As Exception
            MessageBox.Show("DGV_Master_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#End Region

#Region "Interface"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        If AddEnable = False Then
            MessageBox.Show("對不起，您沒有新增權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

        SelectObject.ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport
        Try
            Me.Cursor = Cursors.WaitCursor
            Select Case Me.TabControl1.SelectedIndex
                Case 0

                    Dim Grid As DataGridView = Me.dg_complaint
                    Dim ds As New DataSet
                    Dim Dtab As DataTable
                    Dtab = DirectCast(Grid.DataSource, DataTable).Copy
                    Dtab.TableName = "Receive"
                    Dim Hash As New Hashtable
                    RemoveHideColumn(Grid, Dtab)
                    Dim export As New ExcelExport()
                    export.ExprotToExcel(Dtab, "投诉記錄匯總表")
                Case 1

                    Dim Grid As DataGridView = Me.DataGridView1
                    Dim ds As New DataSet
                    Dim Dtab As DataTable
                    Dtab = DirectCast(Grid.DataSource, DataTable).Copy
                    Dtab.TableName = "Receive"
                    RemoveHideColumn(Grid, Dtab)
                    Dim export As New ExcelExport()
                    Dim title As String = String.Format("查詢送貨記錄表({0})", Me.cbxDeliveryType.Text.Trim)

                    export.ExprotToExcel(Dtab, title)

                Case 2
                    Dim Grid As DataGridView = Me.dgv_3
                    Dim ds As New DataSet
                    Dim Dtab As DataTable
                    Dtab = DirectCast(Grid.DataSource, DataTable).Copy
                    Dtab.TableName = "Receive"
                    RemoveHideColumn(Grid, Dtab)
                    Dim export As New ExcelExport()
                    'Dim title As String = String.Format("(缺貨通知記錄表{0})", Me.cbxDeliveryType.Text.Trim)
                    export.ExprotToExcel(Dtab, "缺貨通知記錄表")

                Case 3

                    Dim Grid As DataGridView = Me.dgv_4
                    Dim ds As New DataSet
                    Dim Dtab As DataTable
                    Dtab = DirectCast(Grid.DataSource, DataTable).Copy
                    Dtab.TableName = "Receive"
                    Dim Hash As New Hashtable
                    RemoveHideColumn(Grid, Dtab)
                    Dim export As New ExcelExport()
                    export.ExprotToExcel(Dtab, "偉倫來電記錄匯總表")
            End Select
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region

End Class
